const threeSums = (nums: Array<number>): Array<[number, number, number]> => {
	let ret: Array<number> = []
	const { length } = nums
	nums.sort((a, b) => a - b)

	for (let i = 0; i < length - 2; i++) {
		let left = i + 1
		let right = length - 1
		if (i > 0 && nums[i] === nums[i - 1]) {
			continue
		}

		while (left < right) {
			if (nums[i] + nums[left] + nums[right] > 0) {
				right--

				if (left < right && nums[right] === nums[right + 1]) {
					right--
				}
			} else if (nums[i] + nums[left] + nums[right] < 0) {
				left++

				if (left < right && nums[left] === nums[left - 1]) {
					left++
				}
			} else {
				ret.push([nums[i], nums[left], nums[right]])
				left++
				right--

				if (left < right && nums[right] === nums[right + 1]) {
					right--
				}

				if (left < right && nums[left] === nums[left - 1]) {
					left++
				}
			}
		}
	}

	return ret
}
